package com.lanou.zakerproject.subscribe;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lanou.zakerproject.R;
import com.lanou.zakerproject.base.VolleySingleton;
import com.lanou3g.greendaodemo.entity.greendao.GridViewEntity;

import java.util.List;

/**
 * Created by Administrator on 2016/1/22.
 */
public class GridViewAdapter extends BaseAdapter {
    private Context context;
    private ChannelSearch channelSearch;
    List<GridViewEntity> gridViewEntityList;
    int hasNet = 1;

    public GridViewAdapter(Context context, ChannelSearch channelSearch,int hasNet) {
        this.context = context;
        this.channelSearch = channelSearch;
        this.hasNet = hasNet;
    }

    public GridViewAdapter(Context context, List<GridViewEntity> gridViewEntityList,int hasNet) {
        this.context = context;
        this.gridViewEntityList = gridViewEntityList;
        this.hasNet = hasNet;
    }

    @Override
    public int getCount() {
        if (hasNet == 1){
            return channelSearch.getData().getList().size();
        }
        return gridViewEntityList.size();
    }

    @Override
    public Object getItem(int position) {
        if (hasNet ==1){
            return channelSearch.getData().getList().get(position);
        }
        return gridViewEntityList.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder myViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_gridview_subscribe, parent, false);
            myViewHolder = new MyViewHolder(convertView);
            convertView.setTag(myViewHolder);
        } else {
            myViewHolder = (MyViewHolder) convertView.getTag();
        }
        if (hasNet ==1) {
            if (channelSearch != null) {
                myViewHolder.textView.setText(channelSearch.getData().getList().get(position).getTitle());
                ImageLoader.ImageListener listener = ImageLoader.getImageListener(myViewHolder.imageView, R.mipmap.iconfont_picpng, R.mipmap.default_img_nonet);
                VolleySingleton.getImageLoader().get(channelSearch.getData().getList().get(position).getPic(), listener);
            }
        }else {
            myViewHolder.textView.setText(gridViewEntityList.get(position).getTitle());
        }
        return convertView;
    }


    class MyViewHolder {
        TextView textView;
        ImageView imageView;

        public MyViewHolder(View view) {
            textView = (TextView) view.findViewById(R.id.item_text_subscribe);
            imageView = (ImageView) view.findViewById(R.id.item_img_subscribe);

        }
    }
}
